package com.easy.generator.vue;

import com.easy.generator.Easy;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;

/**
 * @author easy
 * @date 2025年07月11日 14:45
 */
public class Vite_Config_Generator {
    private static final String DIR_NAME = Easy.VUE_LOCAL_PATH;
    private static final String FILE_NAME = "vite.config.js";
    private static final String FILE_TEMPLATE =
            "import { fileURLToPath, URL } from 'node:url'\n" +
                    "\n" +
                    "import { defineConfig } from 'vite'\n" +
                    "import vue from '@vitejs/plugin-vue'\n" +
                    "import vueDevTools from 'vite-plugin-vue-devtools'\n" +
                    "\n" +
                    "// https://vite.dev/config/\n" +
                    "export default defineConfig({\n" +
                    "  plugins: [\n" +
                    "    vue(),\n" +
                    "    vueDevTools(),\n" +
                    "  ],\n" +
                    "  resolve: {\n" +
                    "    alias: {\n" +
                    "      '@': fileURLToPath(new URL('./src', import.meta.url))\n" +
                    "    },\n" +
                    "  },\n" +
                    "  server: {\n" +
                    "    open: true,\n" +
                    "    host: 'localhost',\n" +
                    "    port: 5173,\n" +
                    "    // 配置代理，实现跨域，发布时需要使用nginx配置\n" +
                    "    proxy: {\n" +
                    "      \"/api\":{\n" +
                    "        target: \"http://localhost:8080\",\n" +
                    "        changeOrigin: true,\n" +
                    "        rewrite: path => path.replace(/^\\/api/, \"/\"),\n" +
                    "      }\n" +
                    "    }\n" +
                    "  }\n" +
                    "})\n";

    public static void generate() throws IOException {
        String content = String.format(FILE_TEMPLATE, LocalDate.now());
        File outputFile = new File( DIR_NAME+ "/" + FILE_NAME);
        if (!outputFile.exists()) {
            outputFile.getParentFile().mkdirs();
            outputFile.createNewFile();
        }
        try (FileWriter writer = new FileWriter(outputFile)) {
            writer.write(content);
        }
        System.out.println("已生成文件：" + outputFile.getAbsolutePath());
    }
}
